package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.pojo.TbSku;
import com.leyou.item.mapper.TbSkuMapper;
import com.leyou.item.service.TbSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * sku表,该表表示具体的商品实体,如黑色的 64g的iphone 8 服务实现类
 * </p>
 *
 * @author leyou
 * @since 2020-02-29
 */
@Service
public class TbSkuServiceImpl extends ServiceImpl<TbSkuMapper, TbSku> implements TbSkuService {

    /**
     * 根据 spuid  查所有的sku
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SkuDTO> findSkusBySpuId(Long spuId) {
        QueryWrapper<TbSku> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbSku::getSpuId, spuId);
        List<TbSku> skus = this.list(wrapper);
        if (CollectionUtils.isEmpty(skus)) {
            throw new LyException(ExceptionEnum.GOODS_NOT_FOUND);
        }
        // 数据转化
        return BeanHelper.copyWithCollection(skus, SkuDTO.class);

    }
}
